package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserRankValue;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/2
 */
public class UserRankValueDaoImpl extends TempDataObjectDao<UserRankValue> {
    public static String getUserRankValueSql = "select * from t_u_rank_value where userId=?";

    public Map<Integer, Map<Integer, UserRankValue>> getUserRankValueMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UserRankValue>> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserRankValueSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserRankValue userRankValue = parse(rs);
                if (!map.containsKey(userRankValue.getType())) {
                    map.put(userRankValue.getType(), new ConcurrentHashMap<>());
                }
                map.get(userRankValue.getType()).put(userRankValue.getChildType(), userRankValue);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateUserRankValueSql = "update t_u_rank_value set value=?,time=?,param=? where userId=? and type=? and childType=?";

    @Override
    protected boolean updateData(UserRankValue userRankValue) {
        userRankValue.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, userRankValue.getValue().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, userRankValue.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRankValue.getParam()));
        para.put(i++, new DbParameter(Types.BIGINT, userRankValue.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userRankValue.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userRankValue.getChildType()));
        if (getDb().executeNoneQuery(updateUserRankValueSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserRankValueSql = "insert into t_u_rank_value(userId,type,childType,value,time,param) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserRankValue userRankValue) {
        userRankValue.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userRankValue.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userRankValue.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, userRankValue.getChildType()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRankValue.getValue().toString()));
        para.put(i++, new DbParameter(Types.BIGINT, userRankValue.getTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, userRankValue.getParam()));
        if (getDb().executeNoneQuery(insertUserRankValueSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserRankValue parseData(ResultSet rs) throws SQLException {
        UserRankValue userRankValue = new UserRankValue();
        userRankValue.setUserId(rs.getLong("userId"));
        userRankValue.setType(rs.getInt("type"));
        userRankValue.setChildType(rs.getInt("childType"));
        userRankValue.setValue(new BigInteger(rs.getString("value")));
        userRankValue.setTime(rs.getLong("time"));
        userRankValue.setParam(rs.getString("param"));
        return userRankValue;
    }
}
